package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import beans.Favourite;
import beans.Restaurant;
import beans.User;

public class FavouriteDAO {

	private HibernateTemplate hibernateTemplate;
	private RestaurantDAO restaurantDAO;
	public void setSessionFactory(SessionFactory sessionFactory){
		this.hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}

	public void saveFavourite (Favourite favourite){
		this.hibernateTemplate.save(favourite);
	}
	public void updateFavourite (Favourite favourite){
		this.hibernateTemplate.update(favourite);
	}
	public void deleteFavourite (Favourite favourite){
		this.hibernateTemplate.delete(favourite);
	}
	public List<Favourite> GetAllFavourite(){
		return this.hibernateTemplate.loadAll(Favourite.class);
	}
	public Favourite findFavourite(User user, Restaurant restaurant){
		Favourite favourite = new Favourite();
		for(Favourite x: GetAllFavourite()){
			if((x.getRestaurant_id()==restaurant.getId()&&(x.getUsername().equalsIgnoreCase(user.getUsername())))){
				favourite.setId(x.getId());
				favourite.setUsername(x.getUsername());
				favourite.setRestaurant_id(x.getRestaurant_id());
				return favourite;
			}
		}
		return null;
	}
	public List<Restaurant> GetAllFavByUsername(String username){
		List<Restaurant> list = new ArrayList<Restaurant>();
		for(Favourite x: GetAllFavourite()){
			if(x.getUsername().equalsIgnoreCase(username)){
				
				list.add(restaurantDAO.findRestaurantById(x.getRestaurant_id()));
			}
		}
		return list;
	}
}
